package com.example.hibernate.controller;

import javax.swing.DefaultListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.example.hibernate.model.DbContext;
import com.example.hibernate.model.entity.Employee;
import com.example.hibernate.view.MainFrame;

public class Application implements Runnable, ListSelectionListener {

	private MainFrame frmMain;
	private DbContext database;

	public void startApplication() {
		SwingUtilities.invokeLater(this);
		this.database = DbContext.getInstance();
	}

	@Override
	public void run() {
		frmMain = new MainFrame();
		frmMain.setVisible(true);
		frmMain.setEmployees(database.getEmployees().getAllEmployees());
		frmMain.setDepartments(database.getDepartments().getAllDepartments());
		frmMain.addTableSelectionListener(this);
		frmMain.gotoFirstRow();
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting()) {
			Employee employee = frmMain.getSelectedEmployee();
			frmMain.updateEmployeeValues(employee);
		}
	}

}
